package Solution;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;

/**
 * 将svg转换为png格式的图片
 * 
 * 
 */
public class Solution {

	/**
	 * 将svg字符串转换为png
	 * 
	 * @param svgCode
	 *            svg代码
	 * @param pngFilePath
	 *            保存的路径
	 * @throws TranscoderException
	 *             svg代码异常
	 * @throws IOException
	 *             io错误
	 */
	public static void convertToPng(String svgCode, String pngFilePath) throws IOException, TranscoderException {

		File file = new File(pngFilePath);

		FileOutputStream outputStream = null;
		try {
			file.createNewFile();
			outputStream = new FileOutputStream(file);
			convertToPng(svgCode, outputStream);
		} finally {
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 将svgCode转换成png文件，直接输出到流中
	 * 
	 * @param svgCode
	 *            svg代码
	 * @param outputStream
	 *            输出流
	 * @throws TranscoderException
	 *             异常
	 * @throws IOException
	 *             io异常
	 */
	public static void convertToPng(String svgCode, OutputStream outputStream) throws TranscoderException, IOException {
		try {
			byte[] bytes = svgCode.getBytes("utf-8");
			PNGTranscoder t = new PNGTranscoder();
		
			TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(bytes));
			TranscoderOutput output = new TranscoderOutput(outputStream);
			t.transcode(input, output);
		
			outputStream.flush();
		} finally {
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void main(String[] args){
        String svgCode = new  String();
//      File file = new File("C:\\svgcon.txt");
//      
//      try {
//          BufferedReader br = new BufferedReader(new FileReader(file));
//          String tempString = null;
//          while((tempString = br.readLine()) != null){
//              svgCode.append(tempString);
//          }
//          System.out.println("svg file content :" + svgCode.toString());
//          br.close();
//      }
//      catch (Exception e1) {
//          // TODO Auto-generated catch block
//          e1.printStackTrace();
//      }
         
        //svgCode.append("<svg width=\"100%" height="100%" version="1.1" xmlns=\"http://www.w3.org/2000/svg"> <circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red"/> </svg>");
       // svgCode.append("<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"473\" height=\"190\" style=\"transform: translate(-235px, -75px);\" viewBox=\"-235 -75 1258 399\"> <g><path stroke-linejoin=\"round\" stroke-linecap=\"round\" stroke=\"#0033ff\" stroke-opacity=\"0.5\" stroke-width=\"15\" fill=\"none\" class=\"leaflet-clickable\" d=\"M404 107L470 174\"/></g> </svg>");
       svgCode=readToString("C:\\test\\test.svg");// 更改svg文件中的width和height ,可以最终改变导出的png图片的大小
        String pngFilePath = "C:\\test\\test.png";
        try {
            convertToPng(svgCode.toString(), pngFilePath);
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (TranscoderException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
	public static String readToString(String fileName) {  
        String encoding = "UTF-8";  
        File file = new File(fileName);  
        Long filelength = file.length();  
        byte[] filecontent = new byte[filelength.intValue()];  
        try {  
            FileInputStream in = new FileInputStream(file);  
            in.read(filecontent);  
            in.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        try {  
            return new String(filecontent, encoding);  
        } catch (UnsupportedEncodingException e) {  
            System.err.println("The OS does not support " + encoding);  
            e.printStackTrace();  
            return null;  
        }  
    }
}